package com.garyliang.alihot;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Keep;
import android.util.Log;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;

public class MainApplication extends Application {


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        initHotfix();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SophixManager.getInstance().queryAndLoadNewPatch();
    }

    private void initHotfix() {
        String appVersion;
        try {
            appVersion = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
        } catch (Exception e) {
            appVersion = "1.0.0";
        }

//        SophixManager.getInstance().setContext(this)
//                .setAppVersion(appVersion)
//                .setAesKey(null)
//                //.setAesKey("0123456789123456")
//                .setEnableDebug(true)
//                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
//                    @Override
//                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
//                        String msg = new StringBuilder("").append("Mode:").append(mode)
//                                .append(" Code:").append(code)
//                                .append(" Info:").append(info)
//                                .append(" HandlePatchVersion:").append(handlePatchVersion).toString();
//                        if (msgDisplayListener != null) {
//                            msgDisplayListener.handle(msg);
//                        } else {
//                            cacheMsg.append("\n").append(msg);
//                        }
//                    }
//                }).initialize();

        // initialize必须放在attachBaseContext最前面，初始化代码直接写在Application类里面，切勿封装到其他类。
        SophixManager.getInstance().setContext(this)
                .setAppVersion(appVersion)
                .setAesKey(null)
                .setEnableDebug(true)
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        // 补丁加载回调通知
                        Log.e("MainApplication", "补丁加载回调通知" + info + " code " + code);
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            Log.e("MainApplication", "表明补丁加载成功");
                            // 表明补丁加载成功
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            Log.e("MainApplication", "表明新补丁生效需要重启");
                            // 表明新补丁生效需要重启. 开发者可提示用户或者强制重启;
                            // 建议: 用户可以监听进入后台事件, 然后调用killProcessSafely自杀，以此加快应用补丁，详见1.3.2.3
                            delayKillProcess();
                        } else {
                            Log.e("MainApplication", "其它错误信息");
                            // 其它错误信息, 查看PatchStatus类说明
                        }
                    }
                }).initialize();
//        // queryAndLoadNewPatch不可放在attachBaseContext 中，否则无网络权限，建议放在后面任意时刻，如onCreate中
//        SophixManager.getInstance().queryAndLoadNewPatch();
    }

    /**
     * 延时4000ms去杀死当前进程
     */
    private void delayKillProcess() {
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                SophixManager.getInstance().killProcessSafely(); //根据阿里的建议,调用其杀掉进程,并且保证了热修复的安全
            }
        }, 2000);
    }

}
